/*jslint browser: true, nomen: true, es5: true*/
/*global $, jQuery, window, requirejs, require, define, Promise*/
(function (factory) {
    "use strict";
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define("lhmcv.AjaxDB", ["jquery"], factory);
	} else {
        // Browser globals
        window.lhmcv = window.lhmcv || {};
		window.lhmcv.AjaxDB = factory($);
	}
}(function ($, noRequireJS) {
    "use strict";
    var AjaxDB;
    function ajax_pass(urlString, action_name, callback, return_data) {
        var global = window.lhmc_mvc_global,
            event_name = "ajax_pass",
            args_list = [return_data],
            event_callback;
        global.debug("ajax pass", urlString, action_name);
        if (callback.hasOwnProperty("trans_pass")) {
            event_callback = new global.Model.EventSingle(
                event_name,
                $.extend({}, callback, {trans_args: args_list})
            );
            event_callback.fire();
        }
    }
    function ajax_fail(urlString, action_name, callback) {
        var global = window.lhmc_mvc_global,
            event_name = "fail",
            event_callback;
        global.warn("ajax fail", urlString, action_name);
        window.alert("有错误发生。详情请查看控制台");
        if (callback.hasOwnProperty("trans_fail")) {
            event_callback = new global.Model.EventSingle(
                event_name,
                $.extend({}, callback, {trans_pass: callback.trans_fail})
            );
            event_callback.fire();
        }
    }
    AjaxDB = function (urlString) {
        this.one_url = urlString + "{pk}/";
        this.all_url = urlString;
    };
    AjaxDB.prototype = {
        /*
        sync: function (callback) {
            var action_name = "sync",
                urlString = this.urlString;
            $.ajax({
                url: urlString,
                type: "POST",
                dataType: "json",
                async: true,
                success: function (result, status) {
                    var return_data = result.data;
                    ajax_pass(urlString, action_name, callback, return_data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    ajax_fail(urlString, action_name, callback);
                }
            });
        },
        */
        _ajax: function (url_str, type_str, send_data) {
            return new Promise(function (resolve, reject) {
                $.ajax({
                    url: url_str,
                    type: type_str,
                    dataType: "json",
                    data: JSON.stringify(send_data),
                    success: function (result, status) {
                        var return_data_list = result.data;
                        resolve(return_data_list);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        reject(XMLHttpRequest.responseText);
                    }
                });
            });
        },
        sync_from_server: function (sync_url_string) {
            return this._ajax(sync_url_string, "POST");
        },
        get: function (pk_val) {
            var get_url_string = this.one_url.format({
                pk: pk_val
            });
            return this._ajax(get_url_string, "GET");
        },
        create: function (data_dict) {
            var create_url_string = this.all_url;
            return this._ajax(create_url_string, "POST", data_dict);
        },
        update: function (pk_val, data_dict) {
            var update_url_string = this.one_url.format({
                pk: pk_val
            });
            return this._ajax(update_url_string, "PUT", data_dict);
        },
        delete: function (pk_val) {
            var delete_url_string = this.one_url.format({
                pk: pk_val
            });
            return this._ajax(delete_url_string, "DELETE");
        }
    };
    return AjaxDB;
}));